#include "stdafx.h"
#include "ToolConfigFile.h"

#include <fstream>
#include <string>

#include "ToolCharType.h"
#include "ToolFileOperation.h"

std::string ReadLineNB(LPCTSTR fileName, int n);

CString LoadProjectName(LPCTSTR fileName)
{
	return ReadLineN(fileName, 1);
}

CString LoadFullBackupPath(LPCTSTR fileName)
{
	return ReadLineN(fileName, 2);
}

CString LoadIncBackupPath(LPCTSTR fileName)
{
	return ReadLineN(fileName, 3);
}

CString LoadDifBackupPath(LPCTSTR fileName)
{
	return ReadLineN(fileName, 4);
}

CString LoadDifincBackupPath(LPCTSTR fileName)
{
	return ReadLineN(fileName, 5);
}

int LoadAutoCycle(LPCTSTR fileName)
{
	return atoi(ReadLineNB(fileName, 6).c_str());
}

int LoadAutoHour(LPCTSTR fileName)
{
	return atoi(ReadLineNB(fileName, 7).c_str());
}

int LoadAutoMin(LPCTSTR fileName)
{
	return atoi(ReadLineNB(fileName, 8).c_str());
}

int LoadIsAutoBackupOnce(LPCTSTR fileName){
	return atoi(ReadLineNB(fileName, 9).c_str());
}


BOOL SaveConfigFile(LPCTSTR fileName, 
					CString projectName,
					CString path1, CString path2, CString path3, CString path4,
					int autoCycle, int autoHour, int autoMin, bool isAutoBackupOnce,
					std::list<CString> fileList)
{
	FILE* file = _tfopen(fileName, _T("w+"));
	if(file == NULL) {
		return FALSE;
	}
	else {
		//char* s = path1.GetBuffer();
		/*_ftprintf(file, _T("%s\n%s\n%s\n%s\n%s\n"),
			projectName.GetBuffer(),
			path1.GetBuffer(), path2.GetBuffer(), path3.GetBuffer(), path4.GetBuffer());*/
		char *s, *s1, *s2, *s3, *s4;
		if(WCharToMByte(projectName.GetBuffer(), s)
			&& WCharToMByte(path1.GetBuffer(), s1)
			&& WCharToMByte(path2.GetBuffer(), s2)
			&& WCharToMByte(path3.GetBuffer(), s3)
			&& WCharToMByte(path4.GetBuffer(), s4))
		{
			fprintf(file, "%s\n%s\n%s\n%s\n%s\n%d\n%d\n%d\n%d\n", s, s1, s2, s3, s4, autoCycle, autoHour, autoMin, isAutoBackupOnce);
		}

		if(s != NULL)
			delete[] s;
		if(s1 != NULL)
			delete[] s1;
		if(s2 != NULL)
			delete[] s2;
		if(s3 != NULL)
			delete[] s3;
		if(s4 != NULL)
			delete[] s4;
		/*fwprintf(file, _T("%s\n%s\n%s\n%s\n%s\n"),
			projectName.GetBuffer(),
			path1.GetBuffer(), path2.GetBuffer(), path3.GetBuffer(), path4.GetBuffer());*/
		/*fprintf(file, ("%s\n%s\n%s\n%s\n%s\n"),
			projectName.GetBuffer(),
			path1.GetBuffer(), path2.GetBuffer(), path3.GetBuffer(), path4.GetBuffer());*/
		std::list<CString>::iterator ite = fileList.begin();
		while(ite != fileList.end()) {
//			_ftprintf(file, _T("%s\n"), (*ite).GetBuffer());
			char* f;
			if(WCharToMByte((*ite).GetBuffer(), f)) {
				fprintf(file, "%s\n", f);
			}
			if(f != NULL) {
				delete[] f;
			}
			++ ite;
		}

		fclose(file);
		return TRUE;
	}
}

BOOL IsCorrectPath(CString path)
{
	if(path.IsEmpty()) {
		return FALSE;
	}
	else {
		return CheckFolderExist(path);
	}
}

BOOL IsCorrectPath2(CString path1, CString path2, CString path3, CString path4)
{
	return IsCorrectPath(path1) && IsCorrectPath(path2) && IsCorrectPath(path3) && IsCorrectPath(path4)
		&& path1 != path2 && path1 != path3 && path1 != path4
		&& path2 != path3 && path2 != path4
		&& path3 != path4;
}

CString ReadLineN(LPCTSTR fileName, int n)
{
	std::ifstream infile;
	infile.open(fileName);
	if(infile.is_open()) {
		std::string sline;
		for(int i = 0; i < n; ++ i) {
			if(infile.eof())
			{
				infile.close();
				return CString();
			}
			getline(infile, sline);
		}
		infile.close();
		//int index=sline.find_last_of("\\",sline.length()-1);
		//sline=sline.substr(0,index);
		return CString(sline.c_str());
	}
	else {
		infile.close();
		return NULL;
	}
}

std::string ReadLineNB(LPCTSTR fileName, int n)
{
	std::ifstream infile;
	infile.open(fileName);
	if(infile.is_open()) {
		std::string sline;
		for(int i = 0; i < n; ++ i) {
			getline(infile, sline);
		}
		if(infile.eof()) {
			infile.close();
			return std::string();
		}
		else {
			infile.close();
			return sline;
		}
	}
	else {
		infile.close();
		return NULL;
	}
}